<?php

include("connect.php");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
function getDateOfRecord($value) {


$blah = substr($value, 4, 4);     // bcdef
$blah =  $blah."-".substr($value, 2, 2);
$blah = $blah."-".substr($value, 0, 2);
  
return $blah;


}

function getPublicationID($pdfname, $dblink) {


$query = sprintf("SELECT `ID` FROM Publication WHERE `Name` = '%s'", mysql_real_escape_string($pdfname));

	 $result = mysql_query($query, $dblink);



	

	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	if (!$result) {
		$message  = 'Invalid query: ' . mysql_error() . "\n";
	    	$message .= 'Whole query: ' . $query;
    		die($message);
	}
$value = "";
if(mysql_num_rows($result)!=0) {
		$value = mysql_result($result, 0); // outputs third employee's name

}
	return $value;
	

}

function insertNewPublication($country, $datepublished, $pdfname, $noticetype, $dblink) {
	


	$blah = getPublicationID($pdfname, $dblink);

	if($blah != "" && $blah != null) {
		return $blah;
	} else {

	$query = sprintf("INSERT INTO Publication (`Name`, `NotificationID`, `DatePublished`, `CountryID`, `AddedOn`) VALUES ('%s', %s, '%s', %s, '%s')", 
				mysql_real_escape_string($pdfname),
				mysql_real_escape_string($noticetype), 
				mysql_real_escape_string($datepublished), 
				mysql_real_escape_string($country),
        gmdate("Y-m-j H:i:s", time() + 3600*12));

	 $result = mysql_query($query, $dblink);

	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	if (!$result) {
		$message  = 'Invalid query: ' . mysql_error() . "\n";
	    	$message .= 'Whole query: ' . $query;
    		die($message);
	}

	return  mysql_insert_id();
}
}

if($_POST["pdf"] != "" && $_POST["pdf"] != NULL) {
$data = $_POST["pdf"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

echo $publicationid;

}

mysql_close($link);
?>
